package com.ch.clinking.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ch.clinking.ClinkingApplication;
import com.ch.clinking.Utils.NetUtils.DoCallBack;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.bean.RememberPassword;
import com.ch.clinking.bean.Shop;
import com.ch.clinking.bean.User;
import com.ch.clinking.databinding.ActivityAddMemberBinding;
import com.ch.clinking.databinding.ActivityEditMerStockBinding;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class AddMemberActivity extends AppCompatActivity {

    private ActivityAddMemberBinding binding;
    private List<Shop> shopList;
    private ClinkingApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAddMemberBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initData();
        initEvent();


    }

    private void initData() {
        app = ClinkingApplication.getInstance();
        shopList = app.shopList;
    }

    private void initEvent() {

        // Populate the shop list
        populateShopList(binding.addMemberShopListContainer);

        // Set the register button click listener
        binding.addMemberBtnRegister.setOnClickListener(v -> {
            String nickname = binding.addMemberEtNickname.getText().toString().trim();
            String username = binding.addMemberEtUsername.getText().toString().trim();
            String password = binding.addMemberEtPassword.getText().toString().trim();
            List<String> selectedShopIds = getSelectedShopIds(binding.addMemberShopListContainer);

            if (username.isEmpty() || password.isEmpty() || nickname.isEmpty() || selectedShopIds.isEmpty()) {
                Toast.makeText(this, "请填写所有信息并选择至少一个店铺", Toast.LENGTH_SHORT).show();
            } else {
                // Handle registration logic here
                // For example, send data to server

                //校验账号
                HashMap<String, String> selectUser = new HashMap<>();
                selectUser.put("account",username);

                OkHttpUtil.getInstance().doPostForm("/user/selectByAccount", new DoCallBack() {
                    @Override
                    public void onSuccess(String result) {

                        User findUser = new Gson().fromJson(result, User.class);
                        if (findUser == null) {
                            Log.d("selectByAccountAddMer", String.valueOf(findUser));

                            // 添加User
                            HashMap<String, Object> addMember = new HashMap<>();

                            User user = new User();
                            user.setAccount(username);
                            user.setNickName(nickname);
                            user.setPassword(password);
                            user.setShopId("2");
                            user.setStatus(1);

                            addMember.put("user", user);
                            addMember.put("shopIdList", selectedShopIds);

                            String addMemberJson = new Gson().toJson(addMember);


                            OkHttpUtil.getInstance().doPostJson("/user/addMember", new DoCallBack() {
                                @Override
                                public void onSuccess(String result) {
                                    Log.d("addMember", "result");

                                    AddMemberActivity.this.finish();

                                }

                                @Override
                                public void onError(Exception e) {
                                    Toast.makeText(AddMemberActivity.this, "网络错误,请稍后再试", Toast.LENGTH_SHORT).show();
                                }
                            }, addMemberJson);


                        } else {
                            Toast.makeText(AddMemberActivity.this, "该账号已注册", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        Toast.makeText(AddMemberActivity.this, "网络错误,请稍后再试", Toast.LENGTH_SHORT).show();
//                handler.postDelayed(LoginActivity.this::finish, 3000);
                    }
                },selectUser);



                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            }
        });


    }

    private void populateShopList(LinearLayout shopListContainer) {
        if (shopList != null) {
            for (Shop shop : shopList) {
                CheckBox checkBox = new CheckBox(this);
                checkBox.setText(shop.getShopName());
                checkBox.setTag(shop.getShopId());
                shopListContainer.addView(checkBox);
            }
        }
    }

    private List<String> getSelectedShopIds(LinearLayout shopListContainer) {
        List<String> selectedShopIds = new ArrayList<>();
        for (int i = 0; i < shopListContainer.getChildCount(); i++) {
            View view = shopListContainer.getChildAt(i);
            if (view instanceof CheckBox) {
                CheckBox checkBox = (CheckBox) view;
                if (checkBox.isChecked()) {
                    selectedShopIds.add((String) checkBox.getTag());
                }
            }
        }
        return selectedShopIds;
    }


}